// pages/security-tel/security-tel.js
var app = getApp();
var com = require('../../common.js')
Page({
  data: {
    old_phone: '',
    password: '',
    phoneNumber: '',
    code: '',
    captcha: '',
    phoneNum: '获取验证码',
    bindEndClick: 'clickEnd',
    number: 61,
    imageCode: '',
    imageImg: '',
    code: '',
    phoneKey: '',
  },
  onLoad: function(options) {
    var _this = this
    _this.setData({
      old_phone: wx.getStorageSync('phone')
    })
    _this.onObtain()
  },
  onReady: function() {},
  onShow: function() {},
  onHide: function() {},
  onUnload: function() {},
  onPullDownRefresh: function() {},
  onReachBottom: function() {},
  // onShareAppMessage: function() {},
  inputPassword(e) {
    var _this = this

  },
  //获取验证码
  onObtain() {
    var _this = this,
      url = app.link.obtain,
      data = {}
    com.link(url, data, function(rep) {
      console.log(rep)
      wx.hideToast()
      _this.setData({
        imageCode: rep.data.response.captcha.key,
        imageImg: rep.data.response.captcha.img,
      })
    }, 'GET')
  },
  //刷新验证码
  f5codeImg() {
    var _ = this
    _.onObtain()
  },
  phoneNumber(e) {
    var _this = this,
      id = e.currentTarget.id
    if (id == 1) {
      _this.setData({
        phoneNumber: e.detail.value
      })
    } else if (id == 2) {
      _this.setData({
        code: e.detail.value
      })
    } else if (id == 3) {
      _this.setData({
        captcha: e.detail.value
      })
    } else if (id == 4) {
      _this.setData({
        password: e.detail.value
      })
    }
  },
  //点击倒计时
  clickEnd() {
    var _this = this,
      data = {
        captcha: _this.data.code,
        phone_number: _this.data.phoneNumber,
        key: _this.data.imageCode,
      },
      url = app.link.basics
    com.link(url, data, function(rep) {
      console.log(rep.data.response.captcha)
      if (rep.data.state == false) {
        com.toast(rep.data.message)
      } else {
        com.toast(rep.data.message)
        _this.setData({
          phoneKey: rep.data.response.key
        })
        _this.onSetTimeout()
      }
    })
  },
  //倒计时
  onSetTimeout() {
    var _this = this
    var numberNum = parseInt(_this.data.number)
    numberNum = numberNum - 1
    if (_this.data.number == 0) {
      _this.setData({
        phoneNum: "重新获取",
        bindEndClick: 'clickEnd',
        number: 61
      })
      return false
    } else {
      _this.setData({
        phoneNum: numberNum + '秒后重新获取',
        bindEndClick: '',
        number: numberNum
      })
    }
    setTimeout(() => {
      _this.onSetTimeout()
    }, 1000)
  },
  comPones(e) {
    var _this = this
    com.link(app.link.infocenter_phone, {
      password: _this.data.password,
      phone_number: _this.data.phoneNumber,
      captcha: _this.data.captcha,
      key: _this.data.phoneKey
    }, function(res) {
      console.log(res)
      if (res.data.state) {
        wx.showToast({
          title: '更换绑定成功',
        })
        setTimeout(() => {
          wx.navigateBack({
            delta: 1
          })
        }, 1500)
      }
    })
  }
})